package com.c.ctgproject.ui.files

import android.content.Intent
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.file.FileWaitPthotoAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.file.SecondExamineCarEntity
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_file_car_wait_photographed.*

/**
 *作者:LiBW
 *创建日期: 2020/10/21
 *描述: 等待拍照车辆列表 拍照列表 页面
 */
class FileCarWaitPhotographedActivity : BaseActivity(),FileWaitPthotoAdapter.CarCallback {

    private val mViewModel: FileCarWaitPhotographedViewModel by lazy { ViewModelProvider(this)[FileCarWaitPhotographedViewModel::class.java] }
    private var page = 1
    private var size = 20
    private var searchText = ""
    private val mList = ArrayList<SecondExamineCarEntity.DataBean.ListBean>()
    private val mAdapter: FileWaitPthotoAdapter by lazy { FileWaitPthotoAdapter(this, mList, this) }
    override fun event() {
        changeTransparentStatusBar()
        mCarRV.adapter = mAdapter
        mCarRV.layoutManager = LinearLayoutManager(this)

        mViewModel.result.observe(this, Observer {
            loading.dismiss()
            mRefresh.over()
            MyLog.d("待拍照车辆列表:$it")
            val data = initData(it)
            if (data.status == 0) {
                val entity = getGson().fromJson(it, SecondExamineCarEntity::class.java).data
                entity?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                showToast(data.msg)
            }
        })
    }

    override fun onResume() {
        super.onResume()
        page=1
        searchText=""
        mList.clear()
        getData()
    }

    override fun click() {
        mSearchET.handlerText {
            if (it.isEmpty()) {
                searchText = ""
                mList.clear()
                page = 1
                getData()
            }
        }

        //搜索
        mSearchTV.setOnClickListener {
            searchText = mSearchET.text.toString()
            page = 1
            mList.clear()
            getData()
        }

        mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }
        mRefresh.setOnRefreshListener {
            page = 1
            mList.clear()
            getData()
        }
    }
    private fun getData() {
        loading.show()
        mViewModel.getCarList(page, size, searchText)
    }
    override fun getLayout() = R.layout.activity_file_car_wait_photographed
    override fun onItemClick(carCode: String) {
        val intent =
            Intent(applicationContext, FileUploadPhotoVideoActivity::class.java)
        intent.putExtra("carCode", carCode)
        startActivity(intent)
    }

}